;(function (_,$,axios) {
    axios.defaults.timeout = 5000;
    // 创建下拉刷新对象
    function PullRefresh(options) {
        return new PullRefresh.prototype.init(options);
    }
    PullRefresh.prototype = {
        constructor:PullRefresh,
        init:function (options) {
            this.containercName = $(options.containercName || ".content");
            this.freshContainercName =$(options.freshContainercName || ".pullRefresh");
            this.startY = 0;
            this.endY = 0;
            // 添加事件
            this.addEvent(options);
            // 当前是否处于加载状态
            this.isLoad = false;
        },
        addEvent:function(options){
            this.freshContainercName.text(options.beforeLoadText);
            this.containercName.css("transition","margin-top 0.2s")
            this.containercName.get(0).addEventListener("touchstart",this.touchstart(options).bind(this));
            this.containercName.on("touchmove",this.touchmove(options).bind(this));
            this.containercName.on("touchend",this.touchend(options).bind(this));
        },
        touchstart:function(options){
            return function(e){
                // 判断是否已经正在加载中 如果是咋不能再次下拉
                if(!this.isLoad){
                    this.startY =  parseInt(e.changedTouches[0].pageY);
                }
            }
        },
        touchmove:function(options){
           return function(e){
               // 判断是否已经正在加载中 如果是咋不能再次下拉
                if(!this.isLoad){
                    this.endY = parseInt(e.changedTouches[0].pageY);
                    if(this.endY - this.startY > 0){
                        this.containercName.css("marginTop",this.endY - this.startY + "px")
                        this.freshContainercName.css("height",this.endY - this.startY + "px")
                    }
                }
           }
        },
        touchend:function(options){
            return function(e){
                // 判断是否已经正在加载中 如果是咋不能再次下拉
                if(!this.isLoad){
                    // 修改状态为正在下拉
                    this.isLoad = true;
                    
                    this.endY = parseInt(e.changedTouches[0].pageY);
                   
                    if(this.endY - this.startY > options.pullOffset && ($.notIeScroll().top === 0 || $.scroll().top === 0)){
                        
                        this.freshContainercName.text(options.loadingText);
                        this.containercName.css({marginTop:'50px'});
                        this.freshContainercName.css({height:"50px"})
                        
                        // 模拟请求 以后就是依赖一个AJAX库 或者自己实现一个发送请求 
                        axios(options.url)
                        .then(function(res){

                            options.success({message:"加载成功",status:200,data:res.data});
                            this.toast("加载成功");
                            setTimeout(function(){
                                $("#_toast_").remove();
                            },1000)
                            this.freshContainercName.text(options.loadedSuccessText);
                            setTimeout(function(){
                                // 修改状态为未加载
                                this.isLoad = false;
                                this.commonPart();
                            
                            }.bind(this),1000)

                        }.bind(this))
                        .catch(function(err){

                            this.commonPart();
                            // 修改状态为未加载
                            this.isLoad = false;
                            this.toast("加载错误");
                            setTimeout(function(){
                                $("#_toast_").remove();
                            },1000)
                            options.error({message:"发生错误",errCode:1,error:err});

                        }.bind(this));
                    
                    }else{
                        options.error({message:"下拉幅度不够",errCode:0});
                        this.commonPart();
                        this.isLoad = false;
                    }
                }
            }
        },
        commonPart:function(){
            this.containercName.css("marginTop","0");
            this.freshContainercName.css("height","0");
        },
        toast:function(message){
             $("body").append($.create("div",message,{id:"_toast_",style:
                "position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);width:100px;height:35px;border:3px solid skyblue;text-align:center;line-height:35px;background-color:black;color:white"})
             )
        }
    };
    PullRefresh.prototype.init.prototype = PullRefresh.prototype;
    // 给Utils进行扩展
    $.extends({pullDownRefresh:PullRefresh})
})(window,Util,axios);